package cn.school.thoughtworks.section2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class PracticeA {
    Map<String,Integer> countSameElements(List<String> collection1) {
        //实现练习要求，并改写该行代码。
       /* Iterator<String> iterator = collection1.iterator();
        String s = iterator.next();
       Map<String,Integer> map=new HashMap<>();
       int i=1;
       while(iterator.hasNext()){
           String next=iterator.next();
           if(next==s){
               map.put(s,++i);
           }else{
               s=next;
               i=1;

           }

       }
        return map;*/
       Map<String,Integer> result=new HashMap<String,Integer>();
       collection1.forEach(
               item->result.put(item,result.get(item)==null? 1 : result.get(item)+1)
       );
       return result;

    }
}
